package com.sdcqjy.property.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.text.TextUtils;
import android.widget.EditText;

import com.sdcqjy.mylibrary.Logger;
import com.sdcqjy.mylibrary.widget.ShowToast;
import com.sdcqjy.property.AppLL;
import com.sdcqjy.property.MyConstants;
import com.sdcqjy.property.R;
import com.sdcqjy.property.base.SimpleBaseActivity;
import com.sdcqjy.property.mode.LoginMode;
import com.sdcqjy.property.presenter.LoginPresenter;
import com.sdcqjy.property.presenter.contract.LoginContract;
import com.sdcqjy.property.utils.GetBookMarkUtils;
import com.sdcqjy.property.wxapi.WChatConstants;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by 磁磁帅 on 2018/3/31.
 * 功能：
 */
public class LoginActivity extends SimpleBaseActivity<LoginPresenter> implements LoginContract.View {
    @BindView(R.id.editTel)
    EditText editTel;
    @BindView(R.id.editPwd)
    EditText editPwd;
    public static final int WChatLogin = 1;
    private BroadcastReceiver receiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (MyConstants.ActionLogin.equals(action)) {
                if (intent.getBooleanExtra("login", false)) finish();
            } else if (MyConstants.ActionOtherLogin.equals(action)) {
                int loginType = intent.getIntExtra("loginType", 0);
                switch (loginType) {
                    case WChatLogin:
                        String code = intent.getStringExtra("code");
                        Logger.v("--code--", code);
                        if (presenter != null) presenter.sendWChatCode(code);
                        break;
                }
            }
        }
    };

    public static void openNew(Context context) {
        Intent intent = new Intent(context, LoginActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    public static void open(Context context) {
        Intent intent = new Intent(context, LoginActivity.class);
        context.startActivity(intent);
    }

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        presenter = new LoginPresenter(this);
        IntentFilter intentFilter = new IntentFilter(MyConstants.ActionOtherLogin);
        intentFilter.addAction(MyConstants.ActionLogin);
        LocalBroadcastManager.getInstance(getActivity()).registerReceiver(receiver, intentFilter);
    }

    protected void asynRun() {
        GetBookMarkUtils.BookMarkMode.clear(getActivity());
    }

    protected void onDestroy() {
        super.onDestroy();
        LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(receiver);
        if (iwxapi != null) {
            iwxapi.unregisterApp();
            iwxapi.detach();
        }
        iwxapi = null;
    }

    @OnClick(R.id.btnLogin)
    public void onBtnLoginClicked() {//登录
        String tel = editTel.getText().toString();
        if (TextUtils.isEmpty(tel)) {
            ShowToast.alertShortOfWhite(getActivity(), R.string.qsryhm);
            return;
        }
        String pwd = editPwd.getText().toString();
        if (TextUtils.isEmpty(pwd)) {
            ShowToast.alertShortOfWhite(getActivity(), R.string.qsrmm);
            return;
        }
        presenter.login(tel, pwd);
    }

    @OnClick(R.id.textReg)
    public void onTextRegClicked() {
        RegActivity.open(getActivity());
    }

    @OnClick(R.id.textSearch)
    public void onTextSearchClicked() {
        CodeLoginActivity.open(getActivity());
    }

    @OnClick(R.id.layoutWChat)
    public void onViewClicked() {
        openidType = WChatLogin;
        iwxapi = WXAPIFactory.createWXAPI(this, WChatConstants.APP_ID, true);
        iwxapi.registerApp(WChatConstants.APP_ID);
        SendAuth.Req req = new SendAuth.Req();
        req.scope = "snsapi_userinfo";
        req.state = "wechat_sdk_demo";
        iwxapi.sendReq(req);
    }

    private int openidType;//openid来源
    private IWXAPI iwxapi;

    public void loginSucc(LoginMode data) {
        ShowToast.alertShortOfWhite(getActivity(), R.string.dengluchenggong);
        AppLL appLL = AppLL.getAppLL();
        appLL.setLogin(true);
        appLL.setLoginMode(data);
        finish();
    }
}
